package com.weizhi.server.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weizhi.server.dto.ChapterDto;
import com.weizhi.server.entity.Chapter;
import com.weizhi.server.mapper.ChapterMapper;
import com.weizhi.server.service.ChapterService;
import com.weizhi.server.util.PropertyCopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ChapterServiceImpl implements ChapterService {
    private final static Logger logger = LoggerFactory.getLogger(ChapterServiceImpl.class);

    @Resource
    private ChapterMapper chapterMapper;

    @Override
    public PageInfo list(JSONObject param) {
        Chapter chapter = new Chapter();
        Long courseId = param.getLong("courseId");
        if(param.getLong("courseId") != null) chapter.setCourseId(courseId);
        PageHelper.startPage(param.getInteger("pageIndex"), param.getInteger("pageSize"));
        List<Chapter> chapterList = chapterMapper.selectSelective(chapter);
        PageInfo pageInfo = new PageInfo(chapterList);
        List<ChapterDto> chapterDtoList = PropertyCopyUtil.copyList(chapterList, ChapterDto.class);
        pageInfo.setList(chapterDtoList);
        return pageInfo;
    }

    @Override
    public void save(Chapter chapter) {
        if(chapter.getId() == null){
            chapterMapper.insertSelective(chapter);
        }else{
            chapterMapper.updateByPrimaryKey(chapter);
        }

    }

    @Override
    public void delete(Long id) {
        chapterMapper.deleteByPrimaryKey(id);
    }
}
